package ruifeng.zcj.dig.ui.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.BaseShowHidActivity;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Entity;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.base.GetCodeActivity;
import ruifeng.zcj.dig.ui.home.MainActivity;
import ruifeng.zcj.dig.ui.register.bean.Code;
import ruifeng.zcj.dig.ui.register.bean.LoginUserBean;
import ruifeng.zcj.mybase.base.BaseActivity;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;
import ruifeng.zcj.mybase.base.utils2.StringUtils;
import ruifeng.zcj.mybase.base.utils2.TDevice;

/**
 * 注册
 * Created by Administrator on 2016/9/18 0018.
 */
public class RegisterActivity extends GetCodeActivity {

    public static final String KEY_CLASS_NAME = "className";
    public static final String KEY_ARGS = "args";

    public static void launch(Activity activity,
                              Class<? extends Fragment> clazz,
                              FragmentArgs args) {
        Intent intent = new Intent(activity, RegisterActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getName());
        if (args != null) {
            intent.putExtra(KEY_ARGS, args);
        }
        activity.startActivity(intent);
    }

    @Bind(R.id.activity_top_left)
    ImageView mActivityTopLeft;
    @Bind(R.id.activity_top_center)
    TextView mActivityTopCenter;
    @Bind(R.id.activity_top_right)
    ImageView mActivityTopRight;
    @Bind(R.id.activity_top_right_text)
    TextView mActivityTopRightText;

    @Bind(R.id.register_et_validate_code)
    EditText mRegisterEtValidateCode;

    @Bind(R.id.register_et_pwd)
    EditText mRegisterEtPwd;
    @Bind(R.id.register_et_rp_pwd)
    EditText mRegisterEtRpPwd;
    @Bind(R.id.register_next)
    TextView mRegisterNext;
    @Bind(R.id.register_cb_ok)
    CheckBox mRegisterCbOk;
    @Bind(R.id.register_go_login)
    TextView mRegisterGoLogin;
    @Bind(R.id.register_first_view)
    LinearLayout mRegisterFirstView;


    private String user_pwd;// 入力的密码


    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        mActivityTopCenter.setText("注册");
        this.time = new TimeCount(120000, 1000);// 构造CountDownTimer对象，为获取验o证码时间段

    }


    @OnClick({R.id.activity_top_left, R.id.register_next, R.id.register_btn_get_code,R.id.register_cb_ok, R.id.register_go_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_btn_get_code:// 获取验证码
                getValidateCode();// 获取验证码
                break;
            case R.id.register_next:// 注册
                register();
                break;
            case R.id.register_cb_ok:// 是否同意
                break;
            case R.id.register_go_login:// 已经有帐号了，马上登录
                goLoginActivity();
                break;
            case R.id.activity_top_left://
                finish();
                break;
        }
    }

    public void goLoginActivity() {
        FragmentArgs fragmentArgs2 = new FragmentArgs();
        fragmentArgs2.add("register", "register");
        ToActivityUtil.toNextActivityAndFinish(RegisterActivity.this, LoginActivity.class, "register", fragmentArgs2);

    }



    @Override
    public String codeType() {
        return "1";
    }

    public void register() {
        if (checkFirstValue()) {
            OSChinaApi.register(this.user_tel, this.user_pwd, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Entity entity = (Entity) AbJsonUtil.fromJson(responseInfo.result, Entity.class);
                    if (entity.OK()) {
                        showSnackBar(getString(R.string.register_sucess));
                        goLoginActivity();
                    } else if (entity.getMsg().equals("fail")) {
                        if (entity.getErrorCode().equals("40001"))
                            showSnackBar(getString(R.string.aleardy_register));
                    }

                }

                @Override
                public void onFailure(HttpException e, String s) {

                }
            });
        }
    }

    private boolean checkFirstValue() {
        // 电话号码
        if (!checkFirstPhone()) {
            return false;
        }

        // 验证码
        String strCode = this.mRegisterEtValidateCode.getText().toString().trim();
        if (TextUtils.isEmpty(strCode) || strCode.length() != 6) {
            showSnackBar(getString(R.string.verification_code_error));
            return false;
        }

        // 验证验证码
        if (code != null) {
            if (!code.getCode().equals(strCode)) {
                showSnackBar(getString(R.string.verification_code_error));
                return false;
            }
        } else {
            showSnackBar(getString(R.string.verification_code_error));
            return false;
        }

        this.user_pwd = this.mRegisterEtPwd.getText().toString().trim();
        String strRpPwd = this.mRegisterEtRpPwd.getText().toString().trim();
        // 密码规则
        if (!this.user_pwd.matches("^[0-9a-zA-Z]{6,20}$")) {
            showSnackBar("密码必须是数字，字母6－20位");
            return false;
        }
        if (!user_pwd.equals(strRpPwd)) {
            showSnackBar(getString(R.string.pwd_do_not_match));
            return false;
        }

        return true;
    }
}
